setwd("../")
rm(list = ls())

# # only need to install once
# devtools::install_github("cloudyr/pyMTurkR", dependencies=TRUE)
# devtools::install_github("Luwei-Ying/validateIt", dependencies=TRUE)
# load R package
library(validateIt)

### load documents with predicted topics
top1domText <- read.csv("textpool/top1domText.csv", stringsAsFactors = FALSE)

OLbroadcursory <- validateLabel(type = "OL", n = 500, 
                                text.predict = top1domText, text.name = "post_text",
                                labels = c("Working Class",
                                           "Planned Parenthood",
                                           "Farm Bill",
                                           "Economy",
                                           "Prescription Medicine",
                                           "Grants for Colleges",
                                           "Banking",
                                           "Government Spending",
                                           "Healthcare",
                                           "Debt Ceiling"), 
                                labels.index = c(1, 9, 21, 35, 44, 62, 65, 70, 88, 89), 
                                labels.add = c("Manufacturing",
                                               "\"Welcome Home\" Messages",
                                               "Islamic Extremists",
                                               "Military Affairs",
                                               "Foreign Affairs",
                                               "Military",
                                               "Military Service",
                                               "\"Thank you\" Messages",
                                               "Counter-terrorism",
                                               "Veterans"))

goldOL <- read.csv("goldstandard/goldOL.csv", stringsAsFactors = FALSE)
OLbroadcursory <- mixGold(tasks = OLbroadcursory, golds = goldOL)
OLbroadcursory <- recordTasks(type = "OL", tasks = OLbroadcursory,
                              path = "labelrecords/OLbroadcursory.Rdata")

# ##### interact with Mturk from here #####
# ### environment
# library(pyMTurkR)
# Sys.setenv(AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID")
# Sys.setenv(AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY")
# options(pyMTurkR.sandbox = T) # Change sandbox = F when ready to run on MTurk
# AccountBalance()
# 
# ### send tasks (first trial)
# OLbroadcursoryids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "OL",
#                                tasksrecord = OLbroadcursory, HITidspath = "HITids/OLbroadcursoryids1.Rdata")
# ### extend HITs if needed
# for(i in OLbroadcursoryids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*26)
# }
# 
# ### get results
# load("HITids/OLbroadcursoryids1.Rdata")
# OLbroadcursoryresults1 <- getResults(batch_id = "OLbroadcursory1", hit_ids = HITids, retry = FALSE)
# write.csv(OLbroadcursoryresults1, "OL/results/OLbroadcursoryresults1.csv", row.names = FALSE)
# evalResults(results = OLbroadcursoryresults1, key = OLbroadcursory, type = "OL")
# 
# ### send tasks (second trial)
# OLbroadcursoryids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "OL",
#                                tasksrecord = OLbroadcursory, HITidspath = "HITids/OLbroadcursoryids2.Rdata")
# ### extend HITs if needed
# for(i in OLbroadcursoryids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*26)
# }
# 
# ### get results
# load("HITids/OLbroadcursoryids2.Rdata")
# OLbroadcursoryresults2 <- getResults(batch_id = "OLbroadcursory2", hit_ids = HITids, retry = FALSE)
# write.csv(OLbroadcursoryresults2, "OL/results/OLbroadcursoryresults2.csv", row.names = FALSE)
# evalResults(results = OLbroadcursoryresults2, key = OLbroadcursory, type = "OL")